package com.lqm.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqm.common.constant.WareConstant;
import com.lqm.common.utils.PageUtils;
import com.lqm.common.utils.Query;
import com.lqm.mall.ware.dao.PurchaseDao;
import com.lqm.mall.ware.entity.PurchaseDetailEntity;
import com.lqm.mall.ware.entity.PurchaseEntity;
import com.lqm.mall.ware.service.PurchaseDetailService;
import com.lqm.mall.ware.service.PurchaseService;
import com.lqm.mall.ware.service.WareSkuService;
import com.lqm.mall.ware.vo.MergeVo;
import com.lqm.mall.ware.vo.PurchaseDoneItemVo;
import com.lqm.mall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {


    @Autowired
    PurchaseDetailService detailService;
    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId=mergeVo.getPurchaseId();
        if(purchaseId==null){
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PruchaseStatusEnum.CREATED.getCode());
            this.save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }
        //TODO 确认采购单状态为1,2
        List<Long> items= mergeVo.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> list=items.stream().map((i)->{
            PurchaseDetailEntity detailEntity=new PurchaseDetailEntity();
            detailEntity.setId(i);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstant.PruchaseDetailStatusEnum.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());

        detailService.updateBatchById(list);
        PurchaseEntity entity=new PurchaseEntity();
        entity.setUpdateTime(new Date());
        entity.setCreateTime(new Date());
        entity.setId(purchaseId);
        this.updateById(entity);


    }

    @Transactional
    @Override
    public void received(List<Long> ids) {
        List<PurchaseEntity> purchaseEntities1=this.listByIds(ids);
        List<PurchaseEntity> purchaseEntities=purchaseEntities1.stream().filter((item)->{
            if(item.getStatus() == WareConstant.PruchaseStatusEnum.CREATED.getCode() ||
                    (item.getStatus()==WareConstant.PruchaseStatusEnum.ASSIGNED.getCode())){
                return true;
            }
            return false;
        }).map((item)->{
            item.setStatus(WareConstant.PruchaseStatusEnum.RECEIVED.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
        //改变采购单的状态 TODO 不知道谁领取了
        this.updateBatchById(purchaseEntities);

        //改变采购项的状态
        purchaseEntities.forEach((item)->{
            List<PurchaseDetailEntity> entities=detailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect=entities.stream().map((entity)->{
                PurchaseDetailEntity entity1=new PurchaseDetailEntity();
                entity1.setId(entity.getId());
                entity1.setStatus(WareConstant.PruchaseDetailStatusEnum.BUYING.getCode());
                return entity1;
            }).collect(Collectors.toList());
            detailService.updateBatchById(collect);
        });



    }


    @Transactional
    @Override
    public void done(PurchaseDoneVo doneVo) {
        //1、改变采购单状态
        Long id= doneVo.getId();
        //2、改变采购项的状态
        Boolean flag=true;
        List<PurchaseDoneItemVo> items= doneVo.getItems();
        List<PurchaseDetailEntity> updates=new ArrayList<>();
        for (PurchaseDoneItemVo item : items) {
            PurchaseDetailEntity detailEntity=new PurchaseDetailEntity();
            if(item.getStatus()==WareConstant.PruchaseDetailStatusEnum.HASERROR.getCode()){
                flag=false;
                detailEntity.setStatus(item.getStatus());
            }else{
                detailEntity.setStatus(WareConstant.PruchaseDetailStatusEnum.FINISH.getCode());
                //3、将采购成功的入库
                PurchaseDetailEntity entity=detailService.getById(item.getItemId());

                wareSkuService.addStock(entity);
            }
            detailEntity.setId(item.getItemId());
            updates.add(detailEntity);
        }
        detailService.updateBatchById(updates);
        //改变采购单状态
        PurchaseEntity purchaseEntity=new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?WareConstant.PruchaseStatusEnum.FINISH.getCode() : WareConstant.PruchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);




    }

}